package com.kk.resource;

import com.kk.resource.http.APIInterface;
import com.kk.resource.http.AppBannerBean;
import com.kk.resource.http.RetrofitUtil;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class Main {

    public static void main(String[] args) {
        // write your code here
        System.out.println("write your code here" + "\r\n");
//        new Main().initHttp();
        new Main().initHttpRxjava();
    }

    private void initHttp() {
        String url = "https://gitee.com/Kevin0319/resource/raw/";
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        APIInterface api = retrofit.create(APIInterface.class);
        Call<ResponseBody> call = api.repo();
        try {
            Response<ResponseBody> bodyResponse = call.execute();
            System.out.println("call = " + call + ", response = " + bodyResponse + "   " + bodyResponse.body().string() + "\r\n");
        } catch (IOException e) {
            e.printStackTrace();
        }

        Call<AppBannerBean> call1 = api.repo1();
        call1.enqueue(new Callback<AppBannerBean>() {
            @Override
            public void onResponse(Call<AppBannerBean> call, Response<AppBannerBean> response) {
                System.out.println("call = " + call + ", response = " + response.body() + "\r\n");
            }

            @Override
            public void onFailure(Call<AppBannerBean> call, Throwable t) {
                System.out.println("call = " + call + ", t = " + t + "\r\n");
            }
        });
    }

    private void initHttpRxjava() {
        String url = "https://gitee.com/Kevin0319/resource/raw/";
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(RetrofitUtil.getHttpClient()) // 打印请求参数
                .build();

        APIInterface api = retrofit.create(APIInterface.class);
        api.repo2().subscribe(new Observer<AppBannerBean>() {
                    @Override
                    public void onComplete() {
                        System.out.println("RxJava onComplete ");
                    }

                    @Override
                    public void onError(Throwable e) {
                        System.out.println("RxJava onError e = " + e);
                    }


                    @Override
                    public void onSubscribe(Disposable d) {
                        System.out.println("RxJava onSubscribe d = " + d.isDisposed());
                    }

                    @Override
                    public void onNext(AppBannerBean appBannerBean) {
                        String appName = appBannerBean.getContent().getApps().get(0).getAppName();
                        System.out.println("RxJava onNext appBannerBean = " + appName);
                    }
                });
    }
}
